<template>
  <el-container class="login">
    <el-header>
      <img src="@/assets/logo.png" alt="logo" style="width: 80px;">
    </el-header>
    <h1>{{ 'CAD - 幼儿评估发展系统' }}</h1>
    <el-main>
      <el-form ref="userInfo"
               label-position="top"
               :model="userInfo"
               :rules="rules"
               label-width="80px">
        <el-form-item prop="username" label="账号">
          <el-input v-model="userInfo.username"></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input type="password" show-password
                    v-model="userInfo.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button :loading="loginIng" type="primary" class="login-btn" @click="login">登录
          </el-button>
          <div v-show="errorMessage !== '' && errorMessage" class="el-form-item__error">
            {{ errorMessage }}
          </div>
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer>
      还没有账号？
      <el-link type="primary" :onclick="toRegister">去注册</el-link>
    </el-footer>
  </el-container>
</template>
<script>

import router from "@/router";
import {validateEMail, validatePhoneTwo, validatePsdReg} from "@/utils/rules";

export default {
  name: 'login',
  data() {
    let passwordValidator = (rule, value, callback) => {
      if (!validatePsdReg(value)) {
        callback(new Error(this.$t('loc.pwdShould8To20Char')));
      }
      callback();
    };
    let accountValidator = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账号'));
      }
      if (!validateEMail(value) && !validatePhoneTwo(value)) {
        callback(new Error(this.$t('loc.InvEmailAddress')));
      }
      callback();
    };
    return {
      loginIng: false,
      errorMessage: '',
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {required: true, validator: accountValidator,  trigger: 'blur'},
        ],
        password: [
          {required: true, validator: passwordValidator,  trigger: 'change'},
        ],
      }
    }
  },
  methods: {
    login() {
      if (this.loginIng) {
        return
      }
      this.$refs['userInfo'].validate((valid) => {
        if (valid) {
          // 获取路由参数
          const redirect = this.$route.query.redirect
          this.loginIng = true
          this.$store.dispatch('loginByUsernameAction', this.userInfo).then(res => {
            this.loginIng = false
            router.push({path: redirect || '/'})
          }).catch(err => {
            console.log('login errorMessage')
            this.loginIng = false
            this.errorMessage = err.response.data.description
          })
        }
      });
    },
    toRegister() {
      router.push({name: 'register'})
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.login {
  text-align: center;
  width: 500px;
  font-size: 18px;
  margin: 0 auto;
  padding: 60px 0;

  .login-btn {
    width: 100%;
    height: 50px;
    font-size: 18px;
  }
}

/deep/ .el-input__wrapper {
  height: 50px;
  font-size: 18px;
}

/deep/ .el-link__inner {
  font-size: 18px;
}
</style>
